package com.yeyks.common.utils.apputil.bean;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.yeyks.common.appcommon.result.CommonPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class MyBeanUtils {

    private final static Logger logger = LoggerFactory.getLogger(MyBeanUtils.class);


    /**
     * ObjectConvert
     *
     * @param source
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T convert(T target, Object source, Class<T> clazz) {
        if (null != source) {
            try {
                if (null == target) {
                    target = clazz.newInstance();
                }
                BeanUtils.copyProperties(source, target);
            } catch (Exception e) {
                logger.warn("convert exception e:{}", e);
                return null;
            }
        } else {
            logger.warn("source is empty");
            return null;
        }
        return target;
    }

    /**
     * ObjectConvert
     *
     * @param source
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T convert(Object source, Class<T> clazz) {
        T target = null;
        if (source != null) {
            target = convert(target, source, clazz);
        }
        return target;
    }

    /**
     * ConvertArrayList
     *
     * @param sourceList
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> convertArrayList(List<?> sourceList, Class<T> clazz) {
        List<T> targetList = new ArrayList<>();
        if (null != sourceList && sourceList.size() > 0) {
            try {
                T eachTarget;
                for (Object eachSource : sourceList) {
                    eachTarget = clazz.newInstance();
                    BeanUtils.copyProperties(eachSource, eachTarget);
                    targetList.add(eachTarget);
                }
            } catch (Exception e) {
                logger.warn("convertArrayList exception e:{}", e);
            }
        } else {
            logger.warn("sourceList is empty");
        }
        return targetList;
    }

    /**
     * ConvertPageInfoList
     *
     * @param sourcePageInfo
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> PageInfo<T> convertPageInfoList(PageInfo<?> sourcePageInfo, Class<T> clazz) {

        PageInfo<T> targetPageInfo = new PageInfo<>();
        targetPageInfo.setPageNum(sourcePageInfo.getPageNum());
        targetPageInfo.setPageSize(sourcePageInfo.getPageSize());
        targetPageInfo.setSize(sourcePageInfo.getSize());
        targetPageInfo.setStartRow(sourcePageInfo.getStartRow());
        targetPageInfo.setEndRow(sourcePageInfo.getEndRow());
        targetPageInfo.setPages(sourcePageInfo.getPages());
        targetPageInfo.setPrePage(sourcePageInfo.getPrePage());
        targetPageInfo.setNextPage(sourcePageInfo.getNextPage());
        targetPageInfo.setIsFirstPage(sourcePageInfo.isIsFirstPage());
        targetPageInfo.setIsLastPage(sourcePageInfo.isIsLastPage());
        targetPageInfo.setHasPreviousPage(sourcePageInfo.isHasPreviousPage());
        targetPageInfo.setHasNextPage(sourcePageInfo.isHasNextPage());
        targetPageInfo.setNavigatePages(sourcePageInfo.getNavigatePages());
        targetPageInfo.setNavigatepageNums(sourcePageInfo.getNavigatepageNums());
        targetPageInfo.setNavigateFirstPage(sourcePageInfo.getNavigateFirstPage());
        targetPageInfo.setNavigateLastPage(sourcePageInfo.getNavigateLastPage());
        targetPageInfo.setTotal(sourcePageInfo.getTotal());
        if (null != sourcePageInfo) {
            targetPageInfo.setList(convertArrayList(sourcePageInfo.getList(), clazz));
        } else {
            logger.warn("sourcePageInfo is null");
        }

        return targetPageInfo;
    }

    public static <T> CommonPage<T> convertCommonPageList(CommonPage<?> sourcePageInfo, Class<T> clazz) {

        CommonPage<T> commonPage = new CommonPage<>();
        if (null != sourcePageInfo) {
            commonPage.setPageNum(sourcePageInfo.getPageNum());
            commonPage.setPageSize(sourcePageInfo.getPageSize());
            commonPage.setTotal(sourcePageInfo.getTotal());
            //处理总数可能查询不成功的问题
            if (sourcePageInfo.getTotal().longValue() != 0L) {
                long totalPage = sourcePageInfo.getTotal() / sourcePageInfo.getPageSize();
                commonPage.setTotalPage((sourcePageInfo.getTotal() % sourcePageInfo.getPageSize()) == 0 ? totalPage : totalPage + 1);
            } else {
                commonPage.setTotalPage(0L);
            }
            if (!CollectionUtils.isEmpty(sourcePageInfo.getList())) {
                commonPage.setList(convertArrayList(sourcePageInfo.getList(), clazz));
            } else {
                commonPage.setList(convertArrayList(new ArrayList<T>(), clazz));
            }
        } else {
            logger.warn("sourcePageInfo is null");
            commonPage.setPageNum(0);
            commonPage.setPageSize(0);
            commonPage.setTotal(0L);
            commonPage.setTotalPage(0L);
            commonPage.setList(Lists.newArrayList());
        }
        return commonPage;

    }


}
